package pfx;


import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Get_Public_from_String {

	/**
	 * 1. 使用Base64对字符串解码，得到 字符数组
	 * 2. 使用X.509Cert 对 字符数组格式化，得到 keyspec
	 * 3. 使用KeyFactory 产生公钥
	 * 
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		String public_String = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAKUl3qnE96SggQyhqSqn/gcr60ncDMw55lG/unlbJrPxqWyIQh6xq/v3z0bqvwjmbhLIjZpI7rBHjYCQhKIGvwM0N8Q5JcelWe1AzHzlbSuCkbFh0jH2ArXF5tm7aacXDOnThcIprbdbz6bYGH8bG3Zra8ScNM4nHI9IwFeMKTwIDAQAB";

		PublicKey publicKey = get_PublicKey(public_String);
		
	}
	public static PublicKey get_PublicKey(String public_String) throws Exception{
		
		byte[] public_byte = Base64.decodeBase64(public_String);
		
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(public_byte);
		
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		
		PublicKey publicKey = keyFactory.generatePublic(keySpec);
		
		return publicKey;
	}

}
